<?php

class GalleryProject implements \Iterator {

    // @var string The id of the project
    private $id;
    // @var string The title of the project
    private $title;
    // @var date   The date of the project
    private $date;
    // @var time   The timestamp of the $date
    private $time;
    // @var array  The tags that describe this project
    private $tags;
    // @var mixed  Either a string description, or a reference to a file to be included as the description
    private $description;
    // @var string The id of the image to use as the cover image
    private $cover;
    // @var GalleryPhoto An array of GalleryPhoto objects
    private $photos = array();

    public function __construct($id, $project) {
        $this->id          = $id;
        $this->title       = $project->title;
        $this->date        = $project->date;
        $this->time        = strtotime($this->date);
        $this->tags        = $project->tags;
        $this->description = $project->description;
        $this->cover       = $project->cover;

        foreach($project->photos as $k => $v) {
            $this->photos[$k] = new GalleryPhoto($k, $v);
        }
    }

    public function getId()    { return $this->id; }
    public function getDate($format='c') { return date($format,$this->time); }
    public function hasTitle() { return isset($this->title) ? TRUE : FALSE; }
    public function getTitle() { return $this->title; }
    public function getTags()  { return $this->tags; }
    public function getThumb() { return $this->photos[$this->cover]->getThumb(); }
    public function getCover() { return $this->photos[$this->cover]->getCover(); }
    public function hasDescription() { return empty($this->description) ? FALSE:TRUE; }
    public function getDescription() {
        if(is_file(c::get('root.web') . $this->description)) return f::read(c::get('root.web') . $this->description);
        return $this->description;
    }
    public function countPhotos() { return count($this->photos); }
    public function getPhoto($id) {
        return $this->photos[$id];
    }
    /**
     * Walk the photos and return the amount asked for
     * If include cover is TRUE, then add it first
     */
    public function getPhotos($limit=FALSE, $includeCover=FALSE) {
        $count = 0;
        $set = array();
        if($includeCover) {
            $set[] = $this->photos[$this->cover];
            $count++;
        }
        foreach($this as $key => $photo) {
            if($key == $this->cover) continue;
            $set[] = $photo;
            if($count++ > $limit) {
                break;
            }
        }
        return $set;
    }

    /**
     * Iterator interface METHODS
     *
     * We'll be iterating over $this->photos, but skipping the cover image since it has it's own unique call
     */
    public function rewind()  { reset($this->photos); }
    public function current() { return current($this->photos); }
    public function next() { next($this->photos); }
    public function key() { key($this->photos); }
    public function valid() {
        $test = current($this->photos);
        return isset($test) && $test instanceOf GalleryPhoto ? TRUE : FALSE;
    }
}
